-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtests_traverse_graph.py
111 lines (96 loc) · 2.67 KB
/
tests_traverse_graph.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import unittest
from traverse_graph_find_connected_components import main as traverse_graph
import sys
from io import StringIO
class TraverseGraphTests(unittest.TestCase):
def test_graph_connected_components_nine_nodes(self):
user_input = """9
3 6
3 4 5 6
8
0 1 5
1 6
1 3
0 1 4
2"""
expected_output = """Connected components: 6 4 5 1 3 0
Connected components: 8 2
Connected components: 7
"""
output = StringIO()
try:
sys.stdin = StringIO(user_input)
sys.stdout = output
traverse_graph()
self.assertEqual(output.getvalue(), expected_output)
finally:
sys.stdin = sys.__stdin__
sys.stdout = sys.__stdout__
def test_graph_connected_components_1_node(self):
user_input = """1
0"""
expected_output = """Connected components: 0
"""
output = StringIO()
try:
sys.stdin = StringIO(user_input)
sys.stdout = output
traverse_graph()
self.assertEqual(output.getvalue(), expected_output)
finally:
sys.stdin = sys.__stdin__
sys.stdout = sys.__stdout__
def test_graph_connected_components_0_nodes(self):
user_input = """0
"""
expected_output = ""
output = StringIO()
try:
sys.stdin = StringIO(user_input)
sys.stdout = output
traverse_graph()
self.assertEqual(output.getvalue(), expected_output)
finally:
sys.stdin = sys.__stdin__
sys.stdout = sys.__stdout__
def test_graph_connected_components_7_nodes(self):
user_input = """7
2 6
1
4
3
1
"""
expected_output = """Connected components: 0
Connected components: 2 6 1
Connected components: 4 3
Connected components: 5
"""
output = StringIO()
try:
sys.stdin = StringIO(user_input)
sys.stdout = output
traverse_graph()
self.assertEqual(output.getvalue(), expected_output)
finally:
sys.stdin = sys.__stdin__
sys.stdout = sys.__stdout__
def test_graph_connected_components_4_vertices(self):
user_input = """4
1 2 3
0 1 2 3
0 1 3
1 2"""
expected_output = """Connected components: 3 2 1 0
"""
output = StringIO()
try:
sys.stdin = StringIO(user_input)
sys.stdout = output
traverse_graph()
self.assertEqual(output.getvalue(), expected_output)
finally:
sys.stdin = sys.__stdin__
sys.stdout = sys.__stdout__
if __name__ == '__main__':
unittest.main()